from pyecharts.charts import Bar, Pie, Radar, Line, Sankey, Kline, Map, Gauge
from pyecharts import options as opts
from config.config import Config

theme = Config.dark_theme


def add_bar_chart():
    bar = (
        Bar(init_opts=opts.InitOpts(theme=theme, width="100%", height="300%"))
        .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
        .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
        .set_global_opts(title_opts=opts.TitleOpts(title="柱状图示例"))
    )
    return bar


def add_line_chart():
    line = (Line(init_opts=opts.InitOpts(theme=theme, width="100%", height="220%"))
            .add_xaxis(["周一", "周二", "周三", "周四", "周五", "周六", "周日"])
            .add_yaxis("最高气温", [11, 11, 15, 13, 12, 13, 10], is_smooth=True)
            .set_global_opts(title_opts=opts.TitleOpts(title="折线图示例"),
                             xaxis_opts=opts.AxisOpts(name="日期"),
                             yaxis_opts=opts.AxisOpts(name="数量")))
    return line


def add_scatter_chart():
    scatter = (
        Bar(init_opts=opts.InitOpts(theme=theme, width="100%", height="220%"))
        .add_xaxis(["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"])
        .add_yaxis("蒸发量", [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
                   label_opts=opts.LabelOpts(is_show=False))
        .add_yaxis("降水量", [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
                   label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="散点图示例"),
            xaxis_opts=opts.AxisOpts(name="月份"),
            yaxis_opts=opts.AxisOpts(name="水量"),
        )
    )
    return scatter


def add_pie_chart():
    pie = (
        Pie(init_opts=opts.InitOpts(theme=theme, width="100%", height="300%"))
        .add(
            series_name="访问来源",
            data_pair=[
                ["直接访问", 335],
                ["邮件营销", 310],
                ["联盟广告", 274],
                ["视频广告", 235],
                ["搜索引擎", 400],
            ],
            radius=["40%", "75%"],
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="饼状图示例"),
            legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
        )
    )
    return pie


def add_radar_chart():
    data = [
        {
            "value": [4300, 10000, 28000, 35000, 50000, 19000],
            "name": "预算分配"
        },
        {
            "value": [5000, 14000, 28000, 31000, 42000, 21000],
            "name": "实际开销"
        }
    ]
    radar = (
        Radar(init_opts=opts.InitOpts(theme=theme, width="100%", height="300%"))
        .add_schema(
            schema=[
                opts.RadarIndicatorItem(name="销售", max_=6500),
                opts.RadarIndicatorItem(name="管理", max_=16000),
                opts.RadarIndicatorItem(name="信息技术", max_=30000),
                opts.RadarIndicatorItem(name="客服", max_=38000),
                opts.RadarIndicatorItem(name="研发", max_=52000),
                opts.RadarIndicatorItem(name="市场", max_=25000),
            ]
        )
        .add(
            series_name="预算分配",
            data=[data[0]["value"]],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
        ).add(
            series_name="实际开销",
            data=[data[1]["value"]],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="雷达图示例"))
    )
    return radar


def add_sankey_chart():
    nodes = [
        {'name': 'Java软件开发'}, {'name': '前端技术'}, {'name': '移动端技术'},
        {'name': '自然语言处理'}, {'name': 'Python数据分析'}, {'name': '数据可视化'},
        {'name': '量化交易'}, {'name': '算法工程'}, {'name': '算法优化'},
        {'name': '机器学习开发'}, {'name': '运维工程'}, {'name': '云计算开发'},
        {'name': '软件UI设计'}, {'name': '计算机视觉'}, {'name': '产品经理'}, {'name': '深度学习'}
    ]
    links = [
        {'source': 'Java软件开发', 'target': '前端技术', 'value': 5},
        {'source': '前端技术', 'target': '移动端技术', 'value': 12},
        {'source': 'Python数据分析', 'target': '自然语言处理', 'value': 10},
        {'source': 'Python数据分析', 'target': '数据可视化', 'value': 8},
        {'source': '自然语言处理', 'target': '算法工程', 'value': 15},
        {'source': '算法工程', 'target': '算法优化', 'value': 15},
        {'source': '自然语言处理', 'target': '算法优化', 'value': 30},
        {'source': '量化交易', 'target': '算法优化', 'value': 10},
        {'source': '自然语言处理', 'target': '移动端技术', 'value': 14},
        {'source': '机器学习开发', 'target': '自然语言处理', 'value': 14},
        {'source': '机器学习开发', 'target': '算法工程', 'value': 20},
        {'source': '软件UI设计', 'target': '移动端技术', 'value': 15},
        {'source': '深度学习', 'target': '计算机视觉', 'value': 12},
        {'source': '计算机视觉', 'target': '算法优化', 'value': 10},
        {'source': '运维工程', 'target': '云计算开发', 'value': 15},
        {'source': '前端技术', 'target': '云计算开发', 'value': 20},
        {'source': '云计算开发', 'target': '产品经理', 'value': 3},
        {'source': '移动端技术', 'target': '产品经理', 'value': 10},
        {'source': '算法优化', 'target': '产品经理', 'value': 7},
        {'source': '计算机视觉', 'target': '产品经理', 'value': 6},
        {'source': '数据可视化', 'target': '产品经理', 'value': 5},
    ]
    sankey = (
        Sankey(init_opts=opts.InitOpts(theme=theme, width="100%", height="300%"))
        .add(
            "sankey",
            nodes=nodes,
            links=links,
            linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color='source'),
            label_opts=opts.LabelOpts(position="right"),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="桑基图示例"))
    )
    return sankey


def add_kline_chart():
    kline = (
        Kline(init_opts=opts.InitOpts(theme=theme, width="100%", height="300%"))
        .add_xaxis(xaxis_data=["2017/7/{}".format(i + 1) for i in range(31)])
        .add_yaxis(
            series_name="日K",
            y_axis=[20, 30, 25, 35, 45, 38, 52, 48, 55, 50, 60, 58, 70, 68, 75, 78, 85, 80, 88, 90, 95, 100, 99, 105,
                    110, 105, 115, 120, 110, 125, 120, 130, 135, 140],
            itemstyle_opts=opts.ItemStyleOpts(color="#ec0000", color0="#00da3c", border_color="#8A0000",
                                              border_color0="#008F28", opacity=0.8),
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(type_="max", name="最大值"),
                      opts.MarkPointItem(type_="min", name="最小值")]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
        .set_global_opts(
            xaxis_opts=opts.AxisOpts(is_scale=True, type_="category", boundary_gap=False),
            yaxis_opts=opts.AxisOpts(
                is_scale=True,
                splitarea_opts=opts.SplitAreaOpts(is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1))
            ),
            datazoom_opts=[opts.DataZoomOpts(type_="slider", range_start=0, range_end=100)],
            title_opts=opts.TitleOpts(title="K线图示例"),
        )
    )
    return kline


def add_map_chart():
    data = [
        ["海门", 9], ["鄂尔多斯", 12], ["招远", 12], ["舟山", 12], ["齐齐哈尔", 14], ["盐城", 15],
        ["赤峰", 16], ["青岛", 18], ["乳山", 18], ["金昌", 19], ["泉州", 21], ["莱西", 21],
        ["日照", 21], ["胶南", 22], ["南通", 23], ["拉萨", 24], ["云浮", 24], ["梅州", 25],
        ["文登", 25], ["上海", 25], ["攀枝花", 25], ["威海", 25], ["承德", 25], ["厦门", 26]]
    map = (
        Map(init_opts=opts.InitOpts(theme=theme, width="100%", height="700%"))
        .add("商家A", [list(z) for z in zip([i[0] for i in data], [i[1] for i in data])], "china")
        .set_global_opts(
            title_opts=opts.TitleOpts(title="地图示例"),
            visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
        )
    )
    return map


def add_gauge_chart():
    gauge = (
        Gauge(init_opts=opts.InitOpts(theme=theme, width="100%", height="300%"))
        .add(
            "", [('转化率',34)]
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="仪表盘示例"),
            tooltip_opts=opts.TooltipOpts(formatter="{a} <br/>{b} : {c}%"),
        )
    )
    return gauge
